/*
 * $Id: Main.java 183 2009-06-04 16:41:01Z meyer@alphatec.de $
 * 
 *
 * Copyright 2009 DeltaDesk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package deltadesk.apps.pictureviewer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import javax.swing.JFrame;

import deltadesk.apps.pictureviewer.io.PictureFilesFilenameFilter;
import deltadesk.apps.pictureviewer.util.DisplayUtil;
import deltadesk.apps.pictureviewer.widget.Box;
import deltadesk.apps.pictureviewer.widget.InfoBar;
import deltadesk.apps.pictureviewer.widget.PictureViewer;

/**
 * Main
 * 
 * @author Andreas Meyer
 * @version $Revision: 183 $
 */
public class Main {
	
	/** The application version. */
	public static final String VERSION = "V0.2";
	
	/** The picture viewer component. */
	private PictureViewer viewer;
	
	/** The top info bar. */
	private final InfoBar infoBar = new InfoBar();

	/** The current index in the files array. */
	private int index;
	
	/** The list of files in the folder. */
	private final File[] files;


	/**
	 * Construct the main class.
	 * 
	 * @param dirStr
	 */
	public Main(final String dirStr) {
		
		File dir = new File(dirStr);
		File f = null;
		if(dir.isFile()) {
			
			f = dir;
			dir = dir.getParentFile();
		}
		files = dir.listFiles(new PictureFilesFilenameFilter());
		
		if(files == null) {
			
			System.err.println("Specified folder not found.");
			System.exit(1);
		} // end if
		
		if(files.length == 0) {
			
			System.err.println("No valid picture found in specified folder.");
			System.exit(1);
		} // end if
		
		Arrays.sort(files);

		// resolve index for given file
		if(f != null) {
			
			final int i = Arrays.binarySearch(files, f);
			if(i >= 0) {
				index = i;
			}
		} // end if
		
		viewer = new PictureViewer();
		setSource(files[index]);

		final JFrame window = new JFrame();
		window.setUndecorated(true);
		window.setBackground(Color.BLACK);
		window.setLayout(new BorderLayout());

		
		final Box infoBarBox = new Box();
		infoBarBox.add(infoBar);
		infoBarBox.setPreferredSize(new Dimension(1, 25));
		infoBarBox.setBorder(null);
		window.getContentPane().add(infoBarBox, BorderLayout.NORTH);
		infoBar.setText1("DeltaDesk Picture Viewer " + VERSION);
		
		window.getContentPane().add(viewer, BorderLayout.CENTER);
		
		final Box buttonBarBox = new Box();
		buttonBarBox.setPreferredSize(new Dimension(1, 25));
		buttonBarBox.setBorder(null);
		window.getContentPane().add(buttonBarBox, BorderLayout.SOUTH);

		
		window.addKeyListener(new KeyAdapter() {

			public void keyPressed(final KeyEvent ke) {
				
				// next
				if(ke.getKeyCode() == KeyEvent.VK_SPACE ||
				   ke.getKeyCode() == KeyEvent.VK_RIGHT) {
					
					if(index+1 < files.length) index++;
					setSource(files[index]);
					
				// previous
				} else if(ke.getKeyCode() == KeyEvent.VK_BACK_SPACE ||
						   ke.getKeyCode() == KeyEvent.VK_LEFT) {
					
					if(index > 0) index--;
					setSource(files[index]);

				// page down
				} else if(ke.getKeyCode() == KeyEvent.VK_PAGE_DOWN) {
					
					index += 10;
					if(index >= files.length) {
						index = files.length - 1;
					}
					setSource(files[index]);

					// page up
				} else if(ke.getKeyCode() == KeyEvent.VK_PAGE_UP) {
					
					index -= 10;
					if(index < 0) {
						index = 0;
					}
					setSource(files[index]);

				// home
				} else if(ke.getKeyCode() == KeyEvent.VK_HOME) {
					
					index = 0;
					setSource(files[index]);

				// end
				} else if(ke.getKeyCode() == KeyEvent.VK_END) {
					
					index = files.length - 1;
					setSource(files[index]);

				// toggle visible state for bars
				} else if(ke.getKeyCode() == KeyEvent.VK_B) {
					
					infoBarBox.setVisible(!infoBarBox.isVisible());
					buttonBarBox.setVisible(!buttonBarBox.isVisible());

				// exit
				} else if(ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
					System.exit(0);
				}
			}
		});
		
		DisplayUtil.hideMouseCursor(window);
		
		final GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
		final GraphicsDevice device = env.getDefaultScreenDevice();
		device.setFullScreenWindow(window);
	}
	
	/**
	 * Set source for picture viewer component.
	 * 
	 * @param file
	 */
	private void setSource(final File file) {
		
		try {
			viewer.setSource(file);
			infoBar.setText2("[" + (index+1) + "/" + files.length + "] " + file.getAbsolutePath());
			infoBar.repaint();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		if (args.length > 0) {
			new Main(args[0]);
		} else {
			System.out.println("Usage: deltadesk-pictureviewer <filename>");
		}

	}

}
